package correct.day8;

import java.util.Arrays;

public class Test2{

    public static int MLS1 (int[] arr) {
        // write code here
        Arrays.sort(arr);

        int i = 0;
        int max = 0;
        while(i < arr.length - 1) {
            int len = 0;
            while(i < arr.length - 1) {
                if (arr[i+1] == arr[i]) {
                    i++;
                }else if (arr[i+1] == arr[i] + 1){
                    i++;
                    len++;
                }else {
                    break;
                }
            }
            max = max > len ? max : len;
        }
        return max+1;
    }

    public static int MLS (int[] arr)
    {
        Arrays.sort(arr);
        int n = arr.length, ret = 0;
        for(int i = 0; i < n; )
        {
            int j = i + 1, count = 1;
            while(j < n)
            {
                if(arr[j] - arr[j - 1] == 1)
                {
                    count++;
                    j++;
                }
                else if(arr[j] - arr[j - 1] == 0)
                {
                    j++;
                }
                else
                {
                    break;
                }
            }
            ret = Math.max(ret, count);
            i = j;
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] arr = {100,4,200,1,3,2};
        System.out.println(MLS(arr));
    }
}
